<?php

namespace Bete\Database;

use Bete\Foundation\ServiceProvider;
use Bete\Database\Model;
use Bete\Database\ConnectionFactory;
use Bete\Database\DatabaseManager;

class DatabaseServiceProvider extends ServiceProvider
{

    public function boot() 
    {
        Model::setConnectionResolver($this->app['db']);
    }

    public function register()
    {
        $this->app->singleton('db.factory', function ($app) {
            return new ConnectionFactory($app);
        });
        $this->app->singleton('db', function ($app) {
            return new DatabaseManager($app, $app['db.factory']);
        });

        $this->app->bind('db.connection', function ($app) {
            return $app['db']->connection();
        });
    }

    public function providers()
    {
        return ['db', 'db.factory', 'db.connection'];
    }

}